<template>
    <j-form-container :disabled="disabled">
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
            <j-vxe-table  keep-source
                            ref="materialunitconvet"
                            :loading="loading"
                            :columns="columns"
                            :dataSource="dataSource"
                            height="auto"
                            :disabled="disabled"
                            :rowNumber="true"
                            :alwaysEdit="true"
                            :rowSelection="true"
                            :toolbar="true"
                            :bordered="true"
                            @selectRowChange="selectRowChange"
                            @valueChange="handleValueChange">

                        </j-vxe-table>
                           
            </a-row>
        </a-form-model>
    </j-form-container>
  </template>
  <script>
import { getAction } from '@/api/manage'
import JFormContainer from '@/components/jeecg/JFormContainer'
import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
import { VALIDATE_FAILED } from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
import { JVXETypes } from '@/components/jeecg/JVxeTable/index'
export default {
  name: 'MaterialUnitConvet',
  mixins: [JVxeTableModelMixin],
  components: {
    JFormContainer,
  },
  props: {
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      dataSource: [],
      columns: [
        {
          title: '计量单位一',
          key: 'firstunitid',
          type: JVXETypes.sel_search,
          dictCode: 'scmuom,uomname,id',
          width: '150px',
          placeholder: '请输入${title}',
          defaultValue: '',
        },
        {
          title: '计量单位一精度',
          key: 'firstjd',
          type: JVXETypes.inputNumber,
          width: '150px',
          placeholder: '请输入${title}',
          defaultValue: '',
        },
        {
          title: '计量单位一数量',
          key: 'firstunitnum',
          type: JVXETypes.inputNumber,
          width: '150px',
          placeholder: '请输入${title}',
          defaultValue: '',
        },
        {
          title: '计量单位二',
          key: 'secunitid',
          type: JVXETypes.sel_search,
          dictCode: 'scmuom,uomname,id',
          width: '150px',
          placeholder: '请输入${title}',
          defaultValue: '',
        },
        {
          title: '计量单位二精度',
          key: 'secjd',
          type: JVXETypes.inputNumber,
          width: '150px',
          placeholder: '请输入${title}',
          defaultValue: '',
        },
        {
          title: '计量单位二数量',
          key: 'secunitnum',
          type: JVXETypes.inputNumber,
          width: '150px',
          placeholder: '请输入${title}',
          defaultValue: '',
        },
        {
          title: '换算规则',
          key: 'convetrule',
          type: JVXETypes.sel_search,
          dictCode:'hsgz',
          width: '150px',
          placeholder: '请输入${title}',
          defaultValue: '',
        },
      ],
      labelCol: {
        xs: { span: 8 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 8 },
        sm: { span: 16 },
      },
      validatorRules: {},
      confirmLoading: false,
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    selectRowChange(event) {
      let { type, action, row, selectedRows, selectedRowIds } = event
      if ((action = 'selected')) {
        this.selectData = selectedRows
      }
    },
    initFormData(url, id,disabled) {
      this.clearFormData()
      this.disabled=disabled;
      if (!id) {
        this.edit(this.modelDefault)
      } else {
        getAction(url, { id: id }).then((res) => {
          if (res.success) {
            this.dataSource=res.result;
          }
        })
      }
    },
    edit(record) {
      this.model = Object.assign({}, record)
    },
    getFormData() {
      let formdata_arr = []
      this.$refs.form.validate((valid) => {
        if (valid) {
          let isNullObj = true
          Object.keys(this.model).forEach((key) => {
            if (this.model[key]) {
              isNullObj = false
            }
          })
          if (!isNullObj) {
            formdata_arr.push(this.model)
          }
        } else {
          this.$emit('validateError', '订单客户表单校验未通过')
        }
      })
    //   return this.dataSource;
      console.log(this.$refs.materialunitconvet.getTableData());
    return this.$refs.materialunitconvet.getTableData();
    },
    validate(index) {
      return new Promise((resolve, reject) => {
        // 验证主表表单
        this.$refs.form.validate((valid) => {
          !valid ? reject({ error: VALIDATE_FAILED, index }) : resolve()
        })
      })
        .then((values) => {
          return Promise.resolve()
        })
        .catch((error) => {
          return Promise.reject(error)
        })
    },
    popupCallback(value, row) {
      this.model = Object.assign(this.model, row)
    },
    clearFormData() {
      this.$refs.form.clearValidate()
    },
  },
}
</script>
  